<template>
  <div>
    <el-divider>附件</el-divider>
    <el-row type="flex" :gutter="20" align="middle">
      <el-col :span="1">
        <el-button type="text" @click="detailsReportDialogHandler('报告详情', 1)">报告详情</el-button>
      </el-col>
      <el-col :span="1" :offset="1">
        <el-button type="text" @click="detailsReportDialogHandler('估价委托书', 5)">估价委托书</el-button>
      </el-col>
      <el-col :span="1" :offset="1">
        <el-button type="text" @click="detailsReportDialogHandler('查勘底表', 2)">查勘底表</el-button>
      </el-col>
      <el-col :span="1" :offset="1">
        <el-button type="text" @click="detailsReportDialogHandler('产证', 3)">产证</el-button>
      </el-col>
      <el-col :span="1" :offset="1">
        <el-button type="text" @click="detailsReportDialogHandler('现场照片', 4)">现场照片</el-button>
      </el-col>
      <el-col :span="1" :offset="1">
        <el-button type="text" @click="detailsReportDialogHandler('协议书', 6)">协议书</el-button>
      </el-col>
      <el-col :span="1" :offset="1">
        <el-button type="text" @click="detailsReportDialogHandler('其他', 7)">其他</el-button>
      </el-col>
    </el-row>
    <el-dialog
      :title="detailsReportDialogTitle"
      :visible.sync="detailsReportDialog"
      width="50%"
      :before-close="detailsReportBeforeCloseHandler"
      destroy-on-close
    >
      <el-form size="small">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="1">
            <el-form-item>
              <file-upload v-if="refreshFileUploadComponent" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="fileList">
        <el-table-column label="文件名" prop="fileName" />
        <el-table-column label="类型" prop="fileType" />
        <el-table-column label="最后修改时间" prop="fileCreateTime" />
        <el-table-column label="文件大小" prop="fileSize" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" :loading="downloadButtonLoading" @click="downloadReportFile(scope.row.fileName)">下载</el-button>
            <el-button type="text" size="small" :loading="deleteFileButtonLoading" @click="deleteReportFile(scope.row.fileName)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>

import FileUpload from '@/views/report-server/report/details-report/component/file-upload'
import { deleteReportFile, downloadReportFile, getReportFileList } from '@/api/file'
export default {
  name: 'FileInfo',
  components: { FileUpload },
  data() {
    return {
      detailsReportDialog: false,
      detailsReportDialogTitle: '',

      typeId: 0,

      fileList: [],

      downloadButtonLoading: false,
      refreshFileUploadComponent: false
    }
  },
  created() {
  },
  methods: {
    detailsReportDialogHandler(val, id) {
      console.log(id)
      const id1 = this.$route.query.id
      getReportFileList(id, id1).then(res => {
        this.fileList = res.data.list
        this.$store.commit('report/SET_FILE_TYPE', id)
        this.detailsReportDialogTitle = val
        this.refreshFileUploadComponent = true
        this.detailsReportDialog = true
      })
    },

    detailsReportBeforeCloseHandler() {
      this.detailsReportDialog = false
      this.detailsReportDialogTitle = ''
      this.refreshFileUploadComponent = false
      // this.$store.commit('report/SET_FILE_TYPE', 0)
      this.fileList = []
    },

    downloadReportFile(val) {
      // 这里按照我的办法来改就行了
      // const typeId = this.typeId
      const typeId = this.$store.getters.fileType
      const id = this.$route.query.id
      this.downloadButtonLoading = true
      downloadReportFile(val, typeId, id).then(res => {
        const url = window.URL.createObjectURL(new Blob([res], { type: res.type }))
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', val)
        document.body.appendChild(link)
        link.click()
        this.downloadButtonLoading = false
      })
    },

    deleteReportFile(val) {
      const typeId = this.$store.getters.fileType
      const id = this.$route.query.id
      this.$confirm('此操作不可逆，确认删除吗？', '删除文件', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteReportFile(val, typeId, id).then(res => {
          this.$message.success(res.message)
          this.detailsReportDialogHandler(val, typeId)
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
